//
//  FaQiShenPiFuJianTableViewCell.m
//  JuBoCRM
//
//  Created by iMac on 2024/3/15.
//  Copyright © 2024 iningKe. All rights reserved.
//

#import "FaQiShenPiFuJianTableViewCell.h"
#import "XieGenJinChuanFuJinTableViewCell.h"
#import "XieGenJinFuJianListTableViewCell.h"
@implementation FaQiShenPiFuJianTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    [_mainTableView registerNib:[UINib nibWithNibName:@"XieGenJinChuanFuJinTableViewCell" bundle:nil] forCellReuseIdentifier:@"XieGenJinChuanFuJinTableViewCell"];
    
    [_mainTableView registerNib:[UINib nibWithNibName:@"XieGenJinFuJianListTableViewCell" bundle:nil] forCellReuseIdentifier:@"XieGenJinFuJianListTableViewCell"];
}


- (void)setModel:(AllModel *)model{
    _model = model;
    _dataArray = model.imageArray;
    if([[[_dataArray lastObject] allValues] count] > 0){
        _mainTableViewHeight.constant = _dataArray.count*52;
    }else{
        _mainTableViewHeight.constant = (_dataArray.count-1)*52+92;
    }
    [_mainTableView reloadData];
    
    [self setupAutoHeightWithBottomView:_mainTableView bottomMargin:0];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dataDic = _dataArray[indexPath.row];
    if([[dataDic allValues] count] > 0){
        XieGenJinFuJianListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XieGenJinFuJianListTableViewCell"];
        NSDictionary *dataDic = _dataArray[indexPath.row];
        cell.nameLabel.text = GetHandleString(dataDic, @"name");
        cell.sizeLabel.text = [self fileSizeConversion:[GetHandleString(dataDic, @"size") integerValue]];
        [cell.xiaZaiButton setTitle:@"删除" forState:UIControlStateNormal];
        cell.manageClickBlock = ^(NSInteger index) {
            //manageType 0 删除  1查看 2添加
            NSInteger type = 0;
            if(index == 101){
                type = 1;
            }
            if(_manageClickBlock){
                _manageClickBlock(dataDic,type);
            }
        };
        return cell;
    }else{
        XieGenJinChuanFuJinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XieGenJinChuanFuJinTableViewCell"];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dataDic = _dataArray[indexPath.row];
    if([[dataDic allValues] count] > 0){
        return 52;
    }
    return 92;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dataDic = _dataArray[indexPath.row];
    if([[dataDic allValues] count] == 0){
        if(_manageClickBlock){
            _manageClickBlock([NSDictionary dictionary],2);
        }
    }
}


//将文件大小转换为 M/KB/B
- (NSString *)fileSizeConversion:(NSInteger)totalSize {
    NSString *totleStr = nil;
    if (totalSize > 1024 * 1024) {
        totleStr = [NSString stringWithFormat:@"%.2fM",totalSize / 1024.00f /1024.00f];
    } else if (totalSize > 1024) {
        totleStr = [NSString stringWithFormat:@"%.2fKB",totalSize / 1024.00f ];
        
    } else {
        totleStr = [NSString stringWithFormat:@"%.2fB",totalSize / 1.00f];
    }
    return totleStr;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
